import { JSEncrypt } from 'jsencrypt';

const PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDPOjysb0vkvtkD
ZqAfCIqOFcFQncc0c1igTCRhNk0EReoBmOvQQiY1LVR6Z+MMl+rARLKmwjKfHPEu
CyrcmqjDyIha3PjjgyjbJteeGLbjHtwfLa9xM924YBJDETSkVBJzLi8lrJT72KEu
J9FBsHlkeT7cQoiM/UG4gaJZBSNtC5oLbhw9LhN18hg/06E6tWMwfJ0BnzTmQeYE
hpdC7QuedDMAevcBoIj2iONLBWNVbMYVA1n5kP907/o6FBXEckmIL7IIQYu+YXAa
JA9350jOJx7qInNdmBZXYT+ow743FpN0RKxmptAeLSFztca3rvOeQi4YME+WMybq
jGQDDVCbJxApcLGpdXvX8HKXnEE/hgXGJ30/pDislsCBZREBWvvZaAb5uM3fb6uK
EpfzGitnGxVckucB+h0Qydx+VG+n49TxJtRxOSZgwwZK00wk3skHWTy2fdcgI8u9
ps18eco/4DDq8eglYcD2m5js+A0MKQKPbwojOVfDmDOovRb/ywEpIkjr4slqdNy8
ch3saFx6NpGSZc8z2iGqbIcDGnmayFHtQYTE1tiY5BJRszbmYOIXAxxOYdtKnaBi
rExQU2hoPJgVQrBwrcklM7WQ6Ses7RZkS0OemlihPHvbE87Hv8eq27/3WqK6Nhmv
CZVTnSisUvP65i+Pob0MkASduYcbLwIDAQABAoICAQCWxSZTxv4OxZQa11X0fkBg
tA9cWg8PaES5C+0EFX/fdgaVdu9LLHWkBwEJtgAJr5YKoXDhxzURcvs+pr1el9/l
4kbi5RboYQset5n02sJlaJR60sBsunG8rZlb1m7YiJqL1rRS0sfqnGhYrnufMYiA
w34qvvWerrxcQ2UdI5Hsyp4fZtOA578c9zERbyZFEqfCJUgOYAka5iM2c74l4VWs
yZo55T1Fn8lGpb3JXXgyNNvBehjCbO6P4/HIw8g6j3RUc28ydo33y+jF/CqM8GeW
JfxmGP6awAaxO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-----`;
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAzzo8rG9L5L7ZA2agHwiK
jhXBUJ3HNHNYoEwkYTZNBEXqAZjr0EImNS1UemfjDJfqwESypsIynxzxLgsq3Jqo
w8iIWtz444Mo2ybXnhi24x7cHy2vcTPduGASQxE0pFQScy4vJayU+9ihLifRQbB5
ZHk+3EKIjP1BuIGiWQUjbQuaC24cPS4TdfIYP9OhOrVjMHydAZ805kHmBIaXQu0L
nnQzAHr3AaCI9ojjSwVjVWzGFQNZ+ZD/dO/6OhQVxHJJiC+yCEGLvmFwGiQPd+dI
zice6iJzXZgWV2E/qMO+NxaTdESsZqbQHi0hc7XGt67znkIuGDBPljMm6oxkAw1Q
mycQKXCxqXV71/Byl5xBP4YFxid9P6Q4rJbAgWURAVr72WgG+bjN32+rihKX8xor
ZxsVXJLnAfodEMncflRvp+PU8SbUcTkmYMMGStNMJN7JB1k8tn3XICPLvabNfHnK
P+Aw6vHoJWHA9puY7PgNDCkCj28KIzlXw5gzqL0W/8sBKSJI6+LJanTcvHId7Ghc
ejaRkmXPM9ohqmyHAxp5mshR7UGExNbYmOQSUbM25mDiFwMcTmHbSp2gYqxMUFNo
aDyYFUKwcK3JJTO1kOknrO0WZEtDnppYoTx72xPOx7/Hqtu/91qiujYZrwmVU50o
rFLz+uYvj6G9DJAEnbmHGy8CAwEAAQ==
-----END PUBLIC KEY-----`;

// 公钥加密
export function encrypt(passwd) {
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(PUBLIC_KEY);
    // 普通的就是return encryptor.encrypt(passwd)
    return encryptor.encrypt(passwd);
}

// 私钥解密
export function decrypt(passwd) {
    const encryptor = new JSEncrypt();
    encryptor.setPrivateKey(PRIVATE_KEY);
    // 普通的就是return decryptor.decrypt(passwd)
    return encryptor.decrypt(passwd);
}

// 长文本加密
export function encryptLong(content) {
    let encryptionSectionSize = 117;
    content = encodeURIComponent(content);
    let ciphertext = '';
    if (content && content.length > encryptionSectionSize) {
        let paragraphic = content.length / encryptionSectionSize;
        let isIntger = (paragraphic | 0) === paragraphic;
        let encryptionCountet = parseInt(paragraphic);
        if (isIntger) {
            for (let i = 0; i < encryptionCountet; i++) {
                ciphertext += encrypt(content.substring(i * encryptionSectionSize, i * encryptionSectionSize + encryptionSectionSize));
            }
        } else {
            let lastContentLength = content.length % encryptionSectionSize;
            for (let i = 0; i < encryptionCountet; i++) {
                ciphertext += encrypt(content.substring(i * encryptionSectionSize, i * encryptionSectionSize + encryptionSectionSize));
            }
            ciphertext += encrypt(content.substring(encryptionCountet * encryptionSectionSize, encryptionCountet * encryptionSectionSize + lastContentLength));
        }
    } else {
        ciphertext = encrypt(content);
    }
    return ciphertext;
}
// 长文本解密
export function decryptLong(content) {
    let decodeSectionSize = 172;
    let decodeText = '';
    let isFinish = false;
    if (content && content.length > decodeSectionSize) {
        let paragraphic = content.length / decodeSectionSize;
        let isIntger = (paragraphic | 0) === paragraphic;
        let encryptionCountet = parseInt(paragraphic);
        if (isIntger) {
            for (let i = 0; i < encryptionCountet; i++) {
                decodeText += decrypt(content.substring(i * decodeSectionSize, i * decodeSectionSize + decodeSectionSize));
                isFinish = decodeText.indexOf('false') !== -1;
                if (isFinish) {
                    return isFinish;
                }
            }
        } else {
            let lastContentLength = content.length % decodeSectionSize;
            for (let i = 0; i < encryptionCountet; i++) {
                decodeText += encrypt(content.substring(i * decodeSectionSize, i * decodeSectionSize + decodeSectionSize));
                isFinish = decodeText.indexOf('false') !== -1;
                if (isFinish) {
                    return isFinish;
                }
            }
            decodeText += encrypt(content.substring(encryptionCountet * decodeSectionSize, encryptionCountet * decodeSectionSize + lastContentLength));
        }
    } else {
        decodeText = decrypt(content);
    }
    return decodeURIComponent(decodeText);
}
